﻿package mortal.game.view.forging.view
{
    import Message.Public.*;
    import com.mui.controls.*;
    import fl.data.*;
    import flash.display.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.forging.*;
    import mortal.game.view.forging.renderer.*;
    import mortal.game.view.forging.util.*;
    import mortal.game.view.palyer.*;
    import mortal.mvc.core.*;

    public class GemPackListSpr extends GSprite
    {
        private var _itemList:GTileList;

        public function GemPackListSpr()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._itemList = UIFactory.tileList(0, 0, 420 + 30, 138 - 46, this);
            this._itemList.columnWidth = 46;
            this._itemList.rowHeight = 46;
            this._itemList.horizontalGap = 0;
            this._itemList.verticalGap = 0;
            this._itemList.setStyle("skin", new Bitmap());
            this._itemList.setStyle("cellRenderer", GemPackItemCellRenderer);
            this.updateItemList();
            this.addListeners();
            return;
        }// end function

        private function addListeners() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.backPackItemUpdateHandler);
            Dispatcher.addEventListener(EventName.ForgingGemSwitchEquip, this.onSelEquipOrCancelHandler);
            Dispatcher.addEventListener(EventName.ResetCurrSelEquip, this.onSelEquipOrCancelHandler);
            return;
        }// end function

        private function removeListeners() : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.backPackItemUpdateHandler);
            Dispatcher.removeEventListener(EventName.ForgingGemSwitchEquip, this.onSelEquipOrCancelHandler);
            Dispatcher.removeEventListener(EventName.ResetCurrSelEquip, this.onSelEquipOrCancelHandler);
            return;
        }// end function

        private function backPackItemUpdateHandler(param1:Object) : void
        {
            this.updateItemList();
            return;
        }// end function

        private function onSelEquipOrCancelHandler(event:DataEvent) : void
        {
            this.updateItemList();
            return;
        }// end function

        private function updateItemList() : void
        {
            var _loc_6:int = 0;
            var _loc_1:* = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
            var _loc_2:* = Cache.instance.pack.backPackCache.getGemByType();
            var _loc_3:* = Cache.instance.pack.backPackCache.getItemsByGroupAndCategory(EGroup._EGroupStuff, EStuff._EPropJewelScroll);
            if (_loc_1 && _loc_1.itemData && GemUtil.isNeedUpdate)
            {
                _loc_2.sort(this.sortOnTypeMatch);
                _loc_3.sort(this.sortOnTypeMatch);
            }
            var _loc_4:* = _loc_2.concat(_loc_3);
            if (_loc_4.length < 18)
            {
                _loc_6 = _loc_4.length;
                while (_loc_6 < 18)
                {
                    
                    _loc_4.push({});
                    _loc_6++;
                }
            }
            var _loc_5:* = new DataProvider();
            _loc_5.addItems(_loc_4);
            this._itemList.dataProvider = _loc_5;
            this._itemList.drawNow();
            GemUtil.isNeedUpdate = true;
            return;
        }// end function

        private function sortOnTypeMatch(param1:ItemData, param2:ItemData) : Number
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_3:* = (GameController.forging.view as ForgingModule).equipDisplaySpr.currSelEquip;
            if (_loc_3 && _loc_3.itemData)
            {
                _loc_4 = _loc_3.itemData.itemInfo.type;
                _loc_5 = EquipJewelMatchConfig.instance.getInfoByType(_loc_4).jeweltype;
            }
            if (param1.itemInfo.type == _loc_5 && param2.itemInfo.type != _loc_5)
            {
                return -1;
            }
            if (param1.itemInfo.type != _loc_5 && param2.itemInfo.type == _loc_5)
            {
                return 1;
            }
            return this.bagItemSort(param1, param2);
        }// end function

        private function bagItemSort(param1:ItemData, param2:ItemData) : int
        {
            if (param2 == null)
            {
                return -1;
            }
            if (param1 == null)
            {
                return 1;
            }
            if (ItemsUtil.isBind(param1))
            {
                if (ItemsUtil.isBind(param2))
                {
                    if (param1.serverData.itemCode > param2.serverData.itemCode)
                    {
                        return -1;
                    }
                    return 1;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                if (ItemsUtil.isBind(param2))
                {
                    return 1;
                }
            }
            return -1;
            return 1;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl();
            this._itemList.dispose();
            this._itemList = null;
            this.removeListeners();
            return;
        }// end function

    }
}
